package com.meichis.yndms.vm;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.meichis.mcsappframework.entity.AppVersion;
import com.meichis.mcsappframework.executor.MCSExecutorService;
import com.meichis.mcsappframework.executor.MCSRunnable;
import com.meichis.mcsappframework.utils.MessageTools;
import com.meichis.mcsappframework.utils.SharePreferenceUtil;
import com.meichis.mcsappframework.utils.Tools;
import com.meichis.yndms.R;
import com.meichis.yndms.model.DSPToken;
import com.meichis.yndms.model.RotateAdv;
import com.meichis.yndms.model.UserInfo;
import com.meichis.yndms.repository.manager.LoginRequestManager;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * @author xmy
 * @title
 * @remake
 * @create 2021-03-08 12:22
 */
public class LoginVM extends MyAndroidViewModel {

    private String userName = "";
    private String passWord = "";
    private Context context;
    private MutableLiveData<UserInfo> userInfo;
    private MutableLiveData<AppVersion> appVersion;
    private MutableLiveData<RotateAdv> rotateAdv;
    private MutableLiveData<DSPToken> dspToken;
    private MutableLiveData<String> errorData;

    public LoginVM(@NonNull Application application) {
        super(application);
        context = application.getApplicationContext();
        userName = SharePreferenceUtil.getInstance().getStringValue(SharePreferenceUtil.PREFERENCES_USERNAME);
        passWord = SharePreferenceUtil.getInstance().getStringValue(SharePreferenceUtil.PREFERENCES_PASSWORD);
    }

    public MutableLiveData<DSPToken> getDspToken() {
        if (dspToken == null) {
            dspToken = new MutableLiveData<>();
        }
        return dspToken;
    }

    public MutableLiveData<RotateAdv> getRotateAdv() {
        if (rotateAdv == null) {
            rotateAdv = new MutableLiveData<>();
        }
        return rotateAdv;
    }

    public MutableLiveData<AppVersion> getAppVersion() {
        if (appVersion == null) {
            appVersion = new MutableLiveData<>();
        }
        return appVersion;
    }

    public MutableLiveData<String> getErrorData() {
        if (errorData == null) {
            errorData = new MutableLiveData<>();
        }
        return errorData;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public MutableLiveData<UserInfo> getUserInfo() {
        if (userInfo == null) {
            userInfo = new MutableLiveData<>();
        }
        return userInfo;
    }

    public void GetCurrentUser() {
        LoginRequestManager.getInstance().GetCurrentUser(getShowDialog(), getUserInfo());
    }

    /**
     * 用户登陆接口
     */
    public void Login() {
        if (TextUtils.isEmpty(userName)) {
            MessageTools.showLongToast("账户不能为空");
            return;
        }

        if (TextUtils.isEmpty(passWord)) {
            MessageTools.showLongToast("密码不能为空");
            return;
        }
        SharePreferenceUtil.getInstance().remove(SharePreferenceUtil.PREFERENCES_AUTHKEY);
        LoginRequestManager.getInstance().Login(getShowDialog(),
                String.valueOf(Tools.getPackageInfo(context).versionCode),
                context.getResources().getString(R.string.app_code),
                Tools.getNetType(context), userName, passWord, getDspToken());
    }

    /**
     * 用户登陆接口
     */
    public void LoginTest() {
        if (TextUtils.isEmpty(userName)) {
            MessageTools.showLongToast("账户不能为空");
            return;
        }

        if (TextUtils.isEmpty(passWord)) {
            MessageTools.showLongToast("密码不能为空");
            return;
        }
        SharePreferenceUtil.getInstance().remove(SharePreferenceUtil.PREFERENCES_AUTHKEY);
        LoginRequestManager.getInstance().changeRootApi();
        LoginRequestManager.getInstance().LoginTest(getShowDialog(),
                String.valueOf(Tools.getPackageInfo(context).versionCode),
                context.getResources().getString(R.string.app_code),
                Tools.getNetType(context), userName, passWord, getDspToken(), getErrorData());
    }

    /**
     * 切换账号时清除本地缓存数据
     */
    public void ClearData() {
        util.remove("sly");
        util.remove(SharePreferenceUtil.PREFERENCES_LASTUPDATETIME);
        util.setObjectValue(SharePreferenceUtil.PREFERENCES_USERINFO, getUserInfo().getValue());
        MCSExecutorService.getInstance().execute(new MCSRunnable() {
            @Override
            protected void doBackground() {
                ImageLoader.getInstance().clearMemoryCache();
                ImageLoader.getInstance().clearDiskCache();

                util.setObjectValue(SharePreferenceUtil.PREFERENCES_USERINFO, getUserInfo().getValue());
                util.setStringValue(SharePreferenceUtil.PREFERENCES_USERNAME, getUserName());
                util.setStringValue(SharePreferenceUtil.PREFERENCES_PASSWORD, getPassWord());

            }

            @Override
            protected void completeOnMain() {
                GetRotateAdvList();
            }
        });
    }

    public void GetRotateAdvList() {
        LoginRequestManager.getInstance().GetRotateAdvList(getShowDialog(), getRotateAdv());
    }

}
